package o;

import android.annotation.TargetApi;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Build.VERSION;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import o.sb.a;

public class xf<T> implements se<T, Bitmap> {
    private static final c ˊ = new c();
    public static final sb<Integer> ˋ = sb.ˋ("com.bumptech.glide.load.resource.bitmap.VideoBitmapDecode.FrameOption", Integer.valueOf(2), new a<Integer>() {
        private final ByteBuffer ˏ = ByteBuffer.allocate(4);

        public void ˊ(@NonNull byte[] bArr, @NonNull Integer num, @NonNull MessageDigest messageDigest) {
            if (num != null) {
                messageDigest.update(bArr);
                synchronized (this.ˏ) {
                    this.ˏ.position(0);
                    messageDigest.update(this.ˏ.putInt(num.intValue()).array());
                }
            }
        }
    });
    public static final sb<Long> ॱ = sb.ˋ("com.bumptech.glide.load.resource.bitmap.VideoBitmapDecode.TargetFrame", Long.valueOf(-1), new a<Long>() {
        private final ByteBuffer ˎ = ByteBuffer.allocate(8);

        public /* synthetic */ void ˊ(@NonNull byte[] bArr, @NonNull Object obj, @NonNull MessageDigest messageDigest) {
            ॱ(bArr, (Long) obj, messageDigest);
        }

        public void ॱ(@NonNull byte[] bArr, @NonNull Long l, @NonNull MessageDigest messageDigest) {
            messageDigest.update(bArr);
            synchronized (this.ˎ) {
                this.ˎ.position(0);
                messageDigest.update(this.ˎ.putLong(l.longValue()).array());
            }
        }
    });
    private final c ʻ;
    private final b<T> ˎ;
    private final uc ˏ;

    @VisibleForTesting
    interface b<T> {
        void ˏ(MediaMetadataRetriever mediaMetadataRetriever, T t);
    }

    @VisibleForTesting
    static class c {
        c() {
        }

        public MediaMetadataRetriever ˎ() {
            return new MediaMetadataRetriever();
        }
    }

    static final class d implements b<AssetFileDescriptor> {
        private d() {
        }

        public /* synthetic */ void ˏ(MediaMetadataRetriever mediaMetadataRetriever, Object obj) {
            ˋ(mediaMetadataRetriever, (AssetFileDescriptor) obj);
        }

        public void ˋ(MediaMetadataRetriever mediaMetadataRetriever, AssetFileDescriptor assetFileDescriptor) {
            mediaMetadataRetriever.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());
        }
    }

    static final class e implements b<ParcelFileDescriptor> {
        e() {
        }

        public /* synthetic */ void ˏ(MediaMetadataRetriever mediaMetadataRetriever, Object obj) {
            ˋ(mediaMetadataRetriever, (ParcelFileDescriptor) obj);
        }

        public void ˋ(MediaMetadataRetriever mediaMetadataRetriever, ParcelFileDescriptor parcelFileDescriptor) {
            mediaMetadataRetriever.setDataSource(parcelFileDescriptor.getFileDescriptor());
        }
    }

    public static se<AssetFileDescriptor, Bitmap> ˎ(uc ucVar) {
        return new xf(ucVar, new d());
    }

    public static se<ParcelFileDescriptor, Bitmap> ˊ(uc ucVar) {
        return new xf(ucVar, new e());
    }

    xf(uc ucVar, b<T> bVar) {
        this(ucVar, bVar, ˊ);
    }

    @VisibleForTesting
    xf(uc ucVar, b<T> bVar, c cVar) {
        this.ˏ = ucVar;
        this.ˎ = bVar;
        this.ʻ = cVar;
    }

    public boolean ˊ(@NonNull T t, @NonNull sc scVar) {
        return true;
    }

    public tx<Bitmap> ˎ(@NonNull T t, int i, int i2, @NonNull sc scVar) throws IOException {
        long longValue = ((Long) scVar.ॱ(ॱ)).longValue();
        if (longValue >= 0 || longValue == -1) {
            Integer valueOf;
            wu wuVar;
            Integer num = (Integer) scVar.ॱ(ˋ);
            if (num == null) {
                valueOf = Integer.valueOf(2);
            } else {
                valueOf = num;
            }
            wu wuVar2 = (wu) scVar.ॱ(wu.ʼ);
            if (wuVar2 == null) {
                wuVar = wu.ʻ;
            } else {
                wuVar = wuVar2;
            }
            MediaMetadataRetriever ˎ = this.ʻ.ˎ();
            try {
                this.ˎ.ˏ(ˎ, t);
                Bitmap ˏ = ˏ(ˎ, longValue, valueOf.intValue(), i, i2, wuVar);
                ˎ.release();
                return wh.ˊ(ˏ, this.ˏ);
            } catch (Throwable e) {
                throw new IOException(e);
            } catch (Throwable th) {
                ˎ.release();
            }
        } else {
            throw new IllegalArgumentException("Requested frame must be non-negative, or DEFAULT_FRAME, given: " + longValue);
        }
    }

    @Nullable
    private static Bitmap ˏ(MediaMetadataRetriever mediaMetadataRetriever, long j, int i, int i2, int i3, wu wuVar) {
        Bitmap bitmap = null;
        if (!(VERSION.SDK_INT < 27 || i2 == Integer.MIN_VALUE || i3 == Integer.MIN_VALUE || wuVar == wu.ॱॱ)) {
            bitmap = ˋ(mediaMetadataRetriever, j, i, i2, i3, wuVar);
        }
        if (bitmap == null) {
            return ॱ(mediaMetadataRetriever, j, i);
        }
        return bitmap;
    }

    @TargetApi(27)
    private static Bitmap ˋ(MediaMetadataRetriever mediaMetadataRetriever, long j, int i, int i2, int i3, wu wuVar) {
        try {
            int parseInt = Integer.parseInt(mediaMetadataRetriever.extractMetadata(18));
            int parseInt2 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(19));
            int parseInt3 = Integer.parseInt(mediaMetadataRetriever.extractMetadata(24));
            if (parseInt3 == 90 || parseInt3 == 270) {
                int i4 = parseInt;
                parseInt = parseInt2;
                parseInt2 = i4;
            }
            float ˎ = wuVar.ˎ(parseInt, parseInt2, i2, i3);
            return mediaMetadataRetriever.getScaledFrameAtTime(j, i, Math.round(((float) parseInt) * ˎ), Math.round(((float) parseInt2) * ˎ));
        } catch (Throwable th) {
            if (Log.isLoggable("VideoDecoder", 3)) {
                Log.d("VideoDecoder", "Exception trying to decode frame on oreo+", th);
            }
            return null;
        }
    }

    private static Bitmap ॱ(MediaMetadataRetriever mediaMetadataRetriever, long j, int i) {
        return mediaMetadataRetriever.getFrameAtTime(j, i);
    }
}
